﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using RentHouse.Admin.Filters;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service;

namespace RentHouse.Admin.Controllers
{  
    [Authorize]
    public class PermissionController : Controller
    {
        private readonly IPermissionService _IPermissionService;
        public PermissionController(IPermissionService permissionService)
        {
            this._IPermissionService = permissionService;
        }
        //[CheckMyPermission("lookPers")]
        public ActionResult Index()
        {
            return View();
        }

        
        public ActionResult GetPerList(int length, int start, string name)
        {
            int count = 0;
            List<PermissionDTO> list = _IPermissionService.GetList(length, start, name,ref count);
            var result = new { data = list, recordsTotal = count, recordsFiltered = count };
            return Json(result, JsonRequestBehavior.AllowGet);
         }
        public ActionResult AddPer()
        {
            return View();
        }

        [CheckMyPermission("addPermission")]
        [HttpPost]
        public ActionResult AddPer(PermissionDTO permission)
        {
            var result = _IPermissionService.AddPermission(permission);
            return Json(result);
        }

        
        [CheckMyPermission("deletPermission")]
        public ActionResult DeletePer(int id)
        {
            var result = _IPermissionService.DeletePermission(id);
            return Json(result);
        
        }
        public ActionResult EditPer(int id)
        {
            PermissionDTO permissionDTO  = _IPermissionService.GetPermission(id);
            return View("AddPer", permissionDTO);

        }

        //[HttpPost]
        //public async Task<ActionResult> EditPer(PermissionDTO permission)
        //{
        //    var result = await _IPermissionService.UpdatePermissionAsync(permission);
        //    return Json(result);

        //}

        [HttpPost]
        public ActionResult EditPer(PermissionDTO permission)
        {
            var result = _IPermissionService.UpdatePermission(permission);
            return Json(result);

        }

        [HttpPost]
        public ActionResult DeletePers(int [] id)
        {

            var result = _IPermissionService.DeletePers(id);
            return Json(result);

        }

    }
}